<template>
  <div class="pd30 bind-select">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>电梯备案审核</div>
      </div>
      <div class="bind-main">
        <div class="pl bind-top flex">
          <div class="flex flexac">
            <div class="flex flexac ">
              <span class="reg-name">注册代码：</span>
              <el-input v-model="parma.registerCode" class="reg-input" type="number" placeholder="请输入注册代码" @input="getBindElevator" />
            </div>
            <div class="rescueCode flex flexac m-l-20">
              <span class="res-name">位置码：</span>
              <el-input v-model="parma.rescueCode" class="res-input co-input" placeholder="请输入位置码" @input="getBindElevator" />
            </div>
            <div class="groupbtn flex m-l-40 ">
              <el-button class="flex flexac m-r-20 fs14" size="small" type="warning" @click="getBindElevator"><i class="co-i" /><span class="search1">查 询</span></el-button>
              <el-button class=" co-empty flexcenter m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清 空</el-button>
            </div>
          </div>
        </div>
        <div class="con-row flex flexac flexjcsb pl">
          <div>
            <el-button class="defaultbtn" type="primary" @click="handleRefresh"><i class="el-icon-refresh" :class="refresh?'goRefresh':''" />刷 新</el-button>

          </div>
          <div class="m-r-25 flex">

            <el-button class="defaultbtn" type="primary" @click="handleApproved"><i class="el-icon-edit" />审核通过</el-button>
            <el-button class="defaultbtn" type="danger" @click="handleNoApproved"><i class="el-icon-edit" />审核不通过</el-button>
            <el-button class="defaultbtn" type="primary" @click="handleExportExcel"><i class="iconfont icon-wenjiandaochu" />导出</el-button>
            <div class="pre m-l-10">
              <el-button class="defaultbtn pre" type="primary" @click="handleSenior('field')"><i class="iconfont icon-lie" />字段
              </el-button>
              <div class="selectUl br1" :class="seniorSet.field?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" :key="i" class="li">
                  <el-checkbox v-model="item.isShow">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <div class="pre m-l-10">
              <el-button class="defaultbtn" type="primary" @click="handleSenior('sortable')"><i class="iconfont icon-paixu" />排序</el-button>
              <div class="selectUl br1" :class="seniorSet.sortable?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" v-if="item.isShow" :key="i" class="li">
                  <el-checkbox v-model="item.sortable">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
          </div>
        </div>
        <el-table
          id="eleRecordData"
          ref="eleRecordData"
          class="con-table"
          type="expand"
          :data="eleRecordData"
          style="width: 100%"
          highlight-current-row
          @selection-change="handleChange"
        >
          <el-table-column width="50" type="selection" label="全选" />

          <template v-for="(item,i) in tablehead">
            <el-table-column v-if="item.isShow" :key="i" align="center" :width="item.width" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
          </template>
          <el-table-column label="绑定状态" align="center" width="100">
            <template slot-scope="scope">
              <span :class="`status-${scope.row.bindState}`">
                {{
                  scope.row.bindState === 0 ? '未绑定' :
                  scope.row.bindState === 1 ? '已录入' :
                  scope.row.bindState === 2 ? '已申请' :
                  scope.row.bindState === 3 ? '已绑定' :
                  scope.row.bindState === 4 ? '已拒绝' : '未确认'
                }}
              </span>
            </template>
          </el-table-column>
          <!-- prop="name" -->
          <el-table-column label="基本操作" align="center">
            <template slot-scope="scope">
              <el-button :disabled="scope.row.bindState !== 2" class="defaultbtn" type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">
                <i class="el-icon-edit" />
                详 情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="paginationWrap flex "
          :current-page="paging.currentPage"
          :page-sizes="paging.pageSizeArr"
          :page-size="paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 详情 -->
    <EleRecordDetail v-model="editData.eleReVisible" :data="editData.data" />
  </div>
</template>

<script>

import EleRecordDetail from './dialog/index'
import { showNotice, formatDateTime } from '@/utils/toolclass'
import { elevatorQueryList, elevatorDetail } from '@/api/elevator/elevator'
import { fetchApplyPassed, fetchApplyRejected } from '@/api/elevator/elevatorRelationship'

export default {
  name: 'EleRecord',
  components: { EleRecordDetail },
  props: {},
  data() {
    return {
      bindVisible: false,
      editData: {
        eleReVisible: false,
        data: null
      },
      examineData: {
        examineVisble: false,
        data: null
      },
      bindSelect: {
        usageStateVal: '',
        usageState: [
          {
            value: '1',
            label: '在用'
          }, {
            value: '2',
            label: '停用'
          }, {
            value: '3',
            label: '不属'
          }, {
            value: '4',
            label: '不明'
          }, {
            value: '5',
            label: '公告注销'
          }, {
            value: '6',
            label: '提交监察'
          }, {
            value: '7',
            label: '正在大修'
          }, {
            value: '8',
            label: '正在安装'
          }, {
            value: '9',
            label: '正在改造'
          }]
      },
      paging: {
        currentPage: 1,
        pageSize: 10,
        pageSizeArr: [10, 20, 50, 100],
        total: 0
      },
      eleRecordData: [],
      parma: {
        bindState: '',
        id: '',
        registerCode: '',
        registerUnit: '',
        rescueCode: '',
        maintenanceUnitId: '',
        useUnitId: ''
      },
      selected: '',
      selectDataLength: 0,
      refresh: false,
      seniorSet: {
        'field': false,
        'sortable': false
      },
      tablehead: [
        {
          index: 0,
          prop: 'index',
          label: '序号',
          width: '70',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'rescueCode',
          label: '位置码',
          width: '80',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'registerCode',
          label: '注册代码',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 3,
          prop: 'usePlaceName',
          label: '场所名称',
          width: '140',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 4,
          prop: 'useUnitName',
          label: '使用单位',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 5,
          prop: 'installAddress',
          label: '安装地址',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 6,
          prop: 'maintenancePerson1',
          label: '第一维保人',
          width: '100',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 7,
          prop: 'maintenancePerson1Phone',
          label: '第一维保人手机号码',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ],
      selectValue: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getBindElevator()
  },
  mounted() {
    this.$storage.set('upUrl', JSON.stringify({ 'path': '/government/safety/recordReview/elevatorRecord' }))
  },
  methods: {
    // 字段 / 排序
    handleSenior(type) {
      if (type === 'field') {
        this.seniorSet.field = !this.seniorSet.field
        this.seniorSet.sortable = false
      }
      if (type === 'sortable') {
        this.seniorSet.field = false
        this.seniorSet.sortable = !this.seniorSet.sortable
      }
    },
    // 导出
    handleExportExcel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('@/excel/Export2Excel')
        const tHeader = ['位置码', '注册代码', '场所名称', '使用单位', '安装地址', '第一维保人姓名', '第一维保人联系电话', '绑定状态']
        const filterVal = ['rescueCode', 'registerCode', 'usePlaceName', 'useUnitName', 'installAddress', 'maintenancePerson1', 'maintenancePerson1Phone', 'bindState']
        const data = this.formatJson(filterVal, this.eleRecordData)
        export_json_to_excel(tHeader, data, '电梯备案审核列表(' + formatDateTime().ymd) + ')'
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    // 刷新
    handleRefresh() {
      this.refresh = true
      this.getBindElevator()
      setTimeout(() => {
        this.refresh = false
      }, 1200)
    },
    // 电梯备案列表
    getBindElevator() {
      elevatorQueryList(2, this.parma.id, this.parma.maintenanceUnitId, this.paging.currentPage, this.paging.pageSize, this.parma.registerCode, this.parma.registerUnit, this.parma.rescueCode, this.parma.useUnitId).then((res) => {
        if (res.code === '000000') {
          this.eleRecordData = res.data.map((item, i) => {
            const items = {
              index: i + 1,
              bindState: item.bindState,
              carNo: item.carNo,
              factoryNo: item.factoryNo,
              id: item.id,
              installAddress: item.installAddress,
              internaNo: item.internaNo,
              maintenancePerson1: item.maintenancePerson1,
              maintenancePerson1Phone: item.maintenancePerson1Phone,
              maintenanceUnitId: item.maintenanceUnitId,
              maintenanceUnitName: item.maintenanceUnitName,
              manufacturerUnitId: item.manufacturerUnitId,
              manufacturerUnitName: item.manufacturerUnitName,
              nextInspectionDate: item.nextInspectionDate,
              operator: item.operator,
              registerCode: item.registerCode,
              registerDate: item.registerDate,
              registerUnit: item.registerUnit,
              registerUpdateDate: item.registerUpdateDate,
              remark: item.remark,
              rescueCode: item.rescueCode,
              usePlaceName: item.usePlaceName,
              useUnitId: item.useUnitId,
              useUnitName: item.useUnitName,
              version: item.version,
              villageId: item.villageId,
              villageName: item.villageName
            }
            return items
          })
          this.paging = {
            pageSize: res.pager.pageSize,
            currentPage: res.pager.pageIndex,
            total: res.pager.totalSize
          }
        }
      })
    },
    // 清空搜索框
    handleEmptySearch() {
      this.parma.registerCode = ''
      this.parma.rescueCode = ''
      this.parma.maintenanceUnitId = ''
      this.parma.useUnitId = ''
      this.getBindElevator()
    },
    // 复选框-多选
    handleChange(val) {
      this.selectValue = val
    },
    // 审核通过
    handleApproved() {
      if (this.selectValue.length < 1) {
        showNotice('warning', '提示', '请勾选备案申请数据')
        return
      } else {
        this.$confirm('确定选中备案通过审核吗？', '备案审核确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '保存',
          cancelButtonText: '取消审核'
        })
          .then(() => {
            this.handlePass(this.selectValue)
            showNotice('success', '提示', '审核通过成功')
            this.handleRefresh()
          })
          .catch(action => {
            showNotice('success', '提示', '审核取消成功')
            this.handleRefresh()
          })
      }
    },
    handlePass(data) {
      const id = data.map(item => item.id)
      fetchApplyPassed({ ids: id }).then(res => {
        this.handleRefresh()
      })
    },
    // 审核不通过
    handleNoApproved() {
      if (this.selectValue.length < 1) {
        showNotice('error', '提示', '请选择备案申请数据')
        return
      } else {
        this.$confirm('确定选中的备案不通过审核吗？', '备案审核确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '保存',
          cancelButtonText: '取消审核'
        })
          .then(() => {
            this.handleNoPass(this.selectValue)
            showNotice('success', '提示', '审核不通过成功')
            this.handleRefresh()
          })
          .catch(action => {
            showNotice('success', '提示', '审核取消成功')
            this.handleRefresh()
          })
      }
    },
    handleNoPass(data) {
      const id = data.map(item => item.id)
      fetchApplyRejected({ ids: id }).then(res => {
        this.handleRefresh()
      })
    },
    // 获取电梯备案详情
    getDetail(row) {
      elevatorDetail(row.id).then(res => {
        if (res.code === '000000') {
          this.detailData = {
            bindState: res.data.bindState, // 绑定状态
            carNo: res.data.carNo, // 厂车牌照编号
            factoryNo: res.data.factoryNo, // 出厂编号
            id: res.data.id, // ID --
            installAddress: res.data.installAddress, // 安装地址
            internaNo: res.data.internaNo, // 单位内部编号
            maintenancePerson1Id: res.data.maintenancePerson1Id, // 第一维保人ID
            maintenancePerson1Name: res.data.maintenancePerson1Name, // 第一维保人姓名
            maintenancePerson1Phone: res.data.maintenancePerson1Phone, // 第一维保人手机号
            maintenancePerson2Id: res.data.maintenancePerson2Id, // 第二维保人ID
            maintenancePerson2Name: res.data.maintenancePerson2Name, // 第二维保人姓名
            maintenancePerson2Phone: res.data.maintenancePerson2Phone, // 第二维保人手机号
            manufacturerUnitId: res.data.manufacturerUnitId, // 生产厂家ID
            manufacturerUnitName: res.data.manufacturerUnitName, // 生产厂家名称
            nextInspectionDate: res.data.nextInspectionDate, // 下次检验日期
            operator: res.data.operator, // 操作人员
            registerCode: res.data.registerCode, // 电梯注册代码
            registerDate: res.data.registerDate, // 注册登记日期
            registerUnit: res.data.registerUnit, // 注册登记机构
            registerUpdateDate: res.data.registerUpdateDate, // 注册更新日期
            remark: res.data.remark, // 备注
            rescueCode: res.data.rescueCode, // 救援代码
            securityPerson: res.data.securityPerson, // 安全管理员姓名
            securityPersonPhone: res.data.securityPersonPhone, // 安全管理员手机
            supportUnit: res.data.supportUnit, // 保养技术支持单位
            usePlaceId: res.data.usePlaceId, // 电梯使用场所ID
            usePlaceName: res.data.usePlaceName, // 使用场合名称
            useUnitId: res.data.useUnitId, // 使用单位ID
            useUnitName: res.data.useUnitName, // 使用单位
            version: res.data.version, // 版本号
            villageId: res.data.villageId, // 小区ID
            villageName: res.data.villageName // 小区名称
          }
        }
      })
    },
    handleEdit(index, row) {
      this.getDetail(row)
      setTimeout(() => {
        this.editData = {
          eleReVisible: true,
          data: this.detailData
        }
      }, 200)
    },
    // 关闭弹窗
    handleCloseDialog() {
      this.editData.eleReVisible = !this.editData.eleReVisible
    },
    handleCloseBindDialog() {
      this.bindVisible = !this.bindVisible
    },
    // 分页
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getBindElevator()
    },
    handleCurrentChange(val) {
      this.paging.currentPage = val
      this.getBindElevator()
    }
  }
}
</script>
<style scoped lang="scss">
.bind-select {
  .bind-main {
    .status-0 {
      color: red;
      font-size: 16px;
      font-weight: 700;
    }
    .status-1 {
      color: darkgoldenrod;
      font-size: 16px;
      font-weight: 700;
    }
    .status-2 {
      color: blue;
      font-size: 16px;
      font-weight: 700;
    }
    .status-3 {
      color: green;
      font-size: 16px;
      font-weight: 700;
    }
    .status-4 {
      color: red;
      font-size: 16px;
      font-weight: 700;
    }
    .bind-top {
      height: 80px;
      background: #eeeeee;
      .reg-name {}
      .res-name {}
      .unit-name {}
      .reg-input {
        width: 200px;
      }
      .co-input {
        width: 180px !important;
      }
    }
  }
}
</style>
